package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherSearch;
import com.atguigu.guli.service.edu.feign.OssFileService;
import com.atguigu.guli.service.edu.mapper.TeacherMapper;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-07-26
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    private OssFileService ossFileService;

    @Override
    public Page<Teacher> selectPageByCondition(Integer pageNum, Integer pageSize, TeacherSearch teacherSearch) {
        Page<Teacher> page = new Page<>(pageNum , pageSize);

        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();

        //判断传入的条件是否存在，如果存在 则设置到queryWrapper中
        String name = teacherSearch.getName();
        Integer level = teacherSearch.getLevel();
        String joinDateBegin = teacherSearch.getJoinDateBegin();
        String joinDateEnd = teacherSearch.getJoinDateEnd();
        if(!StringUtils.isEmpty(name)){
            queryWrapper.likeRight(Teacher::getName ,name);// name%   mysql 索引 如果模糊查询  以%开始 会导致索引失效
        }
        if(level!=null){
            queryWrapper.eq(Teacher::getLevel , level);
        }
        if(!StringUtils.isEmpty(joinDateBegin)){
            queryWrapper.ge(Teacher::getJoinDate , joinDateBegin );// 2021-01-01  2019-12-01
        }
        if(!StringUtils.isEmpty(joinDateEnd)){
            queryWrapper.le(Teacher::getJoinDate , joinDateEnd );
        }
        baseMapper.selectPage(page , queryWrapper);
        return page;
    }

    @Override
    public boolean deleteAvatarById(String id) {
        Teacher teacher = baseMapper.selectById(id);
        if (teacher != null){
            String avatar = teacher.getAvatar();
            if (!StringUtils.isEmpty(avatar)){
                //删除图片
                R r = ossFileService.deleteFile(avatar,"avatar");
                return r.getSuccess();
            }
        }
        return false;
    }

    @Cacheable(value = "index",key = "'selectHotTeacher'")
    @Override
    public List<Teacher> selectHotTeacher() {

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        queryWrapper.last("limit 4");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean batchDelete(List<String> ids) {
        List<String> pathList = new ArrayList<>();
        for (String id : ids) {
            Teacher teacher = baseMapper.selectById(id);
            if (teacher != null){
                String avatar = teacher.getAvatar();
                if (!StringUtils.isEmpty(avatar)){
                    pathList.add(avatar);
                }
            }
        }

        R r = ossFileService.batchDelete(pathList, "avatar");
        return r.getSuccess();

    }
}
